//
//  AppDelegate.swift
//  JonSnowCore
//
//  Created by seongbrave on 03/16/2017.
//  Copyright (c) 2017 seongbrave. All rights reserved.
//

import UIKit
import Foundation
import JonSnowCore
import CommonCore
import NetWorkCore
import SwiftyUserDefaults
import UtilCore
import BrandonCore

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        setCommonAppleance()
        setupURLNavigator(launchOptions)   //注册app路由
        setNetCoreConfing()
        self.setupRootViewController()
        // Override point for customization after application launch.
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}
extension AppDelegate{
    
    /**
     设置导航等 的主题颜色
     */
    internal func setCommonAppleance(){
        
        UIApplication.shared.statusBarStyle = .lightContent
        let navigationBarAppearance = UINavigationBar.appearance()
        //设置显示的颜色
        navigationBarAppearance.barTintColor = UIColor.white
        navigationBarAppearance.tintColor = UIColor.black
        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.black]
        
        let tabBarAppearance = UITabBar.appearance()
        tabBarAppearance.barTintColor = UIColor.white
        tabBarAppearance.isOpaque = true
        
    }
    /// 修改NetWorkCore 网络相关配置
    func setNetCoreConfing() {
        
        NetWorkCore.baseUrl = ["http://10.0.0.13:9085"]
        let devid = Defaults[.uuid]
        if devid.characters.count > 0 {
            UtilCore.sharedInstance.devid = devid
            NetWorkAPI.sharedInstance.headers["devid"] = devid
        }
        NetWorkAPI.sharedInstance.headers["apptype"] = "iOS"
        NetWorkAPI.sharedInstance.headers["appversion"] = "99.99.99"
    }
    ///app路由注册
    internal func setupURLNavigator(_ launchOptions: [UIApplicationLaunchOptionsKey: Any]?){
        
        MainNavigatorMap.initialize()
        
        if let url = launchOptions {
            
            print(url)
        }
    }
    
}

extension AppDelegate{
    /**
     跳转到登陆界面
     */
    internal func toLoginVc(){
        let loginNav = UINavigationController(rootViewController: JonSnowCore.loginVc)
        window!.rootViewController = loginNav
    }
    /// 设置tabbarcontroller
    internal func setTabBarVc() ->  Void{
        
    }
    /**
     根控制器
     1、firstPage:false第一次登陆 则跳转到引导界面
     2、已经登陆过 即firstPage 为true ,hasLogined :false 表示没有登陆成功，需要跳转到登陆界面
     3、如果hasLogined:false 则需要判断LoginedUser的user是否有缓存数据，如果没有则还需要跳转到登陆界面 否则就可以直接跳转到首页
     */
    internal func setupRootViewController() {
        self.window = UIWindow(frame: UIScreen.main.bounds);
        self.window!.backgroundColor=UIColor.white;
        let  firstPage = Defaults[.firstPage]
        /// 登陆成功后 点击了进入觅客商城按钮 则设置为true 否则为false
        let  hasLogined = Defaults[.hasLogined]
        let user = AppData.loginedUser
        ///表示第一次登陆
        if firstPage == false {
            window?.rootViewController =  JonSnowCore.guideVc
            self.window?.makeKeyAndVisible()
        }else if user != nil && hasLogined{
            self.setTabBarVc()
        }else{
            self.toLoginVc()
        }
    }
}

